package com.kbt.util.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UpLoadImage {
    private static int statusCode;
    private static String groupId="";


    public static String formUpload(String urlStr, String filePath) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urlStr);
            File file = new File(filePath);
            String fileName = file.getName();
            String fileExtend = fileName.substring(fileName.lastIndexOf('.') + 1);
            FileBody fileBody = new FileBody(file, "image/" + fileExtend);
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("file", fileBody);
            httpPost.setEntity(entity);
            /* 发送请求并等待响应 */
            HttpResponse httpResponse = httpclient
                    .execute(httpPost);
            statusCode = httpResponse.getStatusLine().getStatusCode();
            // 若状态码为200 ok
            if (200 == statusCode) {
                String strResult = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                JSONObject jsonObject = null;
                try {
                    /**把json字符串转换成json对象**/
                    jsonObject = new JSONObject(strResult);
                    String names = "";
                    String data = jsonObject.getString("data");
                    JSONObject jDat = new JSONObject(data);
                    groupId = jDat.getString("groupId");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
            }
            return groupId;
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return groupId;
    }

}
